package com.leyou.item.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.dao.ManagerMapper;
import com.leyou.item.pojo.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author:yanjizhw@163.com
 * @date:2019-10-10 16:30
 * 描述:
 * @since:1.0.0
 */
@Service
public class ManagerService {

    @Autowired
    private ManagerMapper managerMapper;


    public PageInfo<Manager> queryManager(Integer page, int pageSize) {

        //分页
        PageHelper.startPage(page, pageSize);

        Example example = new Example(Manager.class);
        Example.Criteria criteria = example.createCriteria();

        //只查询未删除
        criteria.andNotEqualTo("status", 2);


        //查询
        List<Manager> managerList = managerMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(managerList)) {
            throw new LyException(ExceptionEnum.MANAGER_NOT_FOUND);
        }

        PageInfo<Manager> pageInfo = new PageInfo<>(managerList);

        return pageInfo;
    }

    public Manager login(Manager manager) {

        Example example = new Example(Manager.class);
        example.createCriteria().andEqualTo("username", manager.getUsername());
        List<Manager> managers = managerMapper.selectByExample(example);
        return managers.size() > 0 ? managers.get(0) : null;
    }
}
